package com.yb.web.member.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import com.yb.base.web.BaseAction;
import com.yb.bussiness.member.MemberService;
import com.yb.bussiness.member.MemberServiceImpl;
import com.yb.domain.goods.TgInformation;
import com.yb.domain.member.TsMember;
import com.yb.domain.member.TsSysuser;
import com.yb.util.comm.Contants;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.comm.RequestUtil;

/**
 * 业主相关Action
 * 
 * @author SFF
 * 
 */
public class MemberAction extends BaseAction {
    
    private static final long serialVersionUID = 688970037309060648L;
    
    private int total;
    
    private TsMember tsMember;
    
    private List rows;
    
    private MemberService service;
    
    public MemberAction() {
        service = new MemberServiceImpl();
    }
    
    public String searchMember() {
        try {
            String page = RequestUtil.getParam(request, "page", "1");
            String row = RequestUtil.getParam(request, "rows", "10");
            String username = RequestUtil.getParam(request, "username", "");
            String startTime = RequestUtil.getParam(request, "startTime", "");
            String endTime = RequestUtil.getParam(request, "endTime", "");
            String type = RequestUtil.getParam(request, "type", "0");
            TsMember formBean = new TsMember();
            formBean.setUsername(username);
            formBean.setEndTime(endTime);
            formBean.setStartTime(startTime);
            formBean.setType(new Byte(type));
            Map<String, Object> map = service.findAll(formBean, page, row);
            rows = (List<TgInformation>)map.get("list");
            total = Integer.parseInt(map.get("total").toString());
        }
        catch (Exception e) {
            log.error("业主管理信息出错", e);
        }
        return Contants.JSONSTRING_RESULT_NAME;
    }
    
    

    public String loadpremind(){
        PrintWriter out;
        try{
            response.reset();
            response.setCharacterEncoding("utf-8");
            out = response.getWriter();
            Integer count = service.loadpremind();
            out.write(count+"");
        }catch (Exception e) {
            log.error("加载新增的申请", e);
        }
        return null;
    }
    
    public String onAutding() {
        try {
            Long id = RequestUtil.getParam(request, "id", 0l);
            int type = RequestUtil.getParam(request, "type", 0);
            service.onAutding(id, type);
            response.getWriter().print("1");
        } catch (Exception e) {
            log.error("审核业主失败", e);
            try {
                response.getWriter().print("2");
            } catch (IOException e1) {
                log.error("审核业主失败", e1);
            }
        }
        return null;
    }
    
    public String read() {
        try {
            TsSysuser tsSysuser = (TsSysuser) RequestUtil
            .getLoginUserFromSession(request, Contants.SESSION_USER);
             Long sysuserid = tsSysuser.getId();
             tsMember = service.read(sysuserid);
             tsMember.setCreatTimeStr(DateTimeUtil.switchDateToString(tsMember.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
             tsMember.setType(new Byte(tsSysuser.getType()));
        } catch (Exception e) {
            log.error("查询业主信息", e);
        }
        return "memberinfo";
    }

    public String saveOrUpdate() {
        try {
            service.saveOrUpdate(tsMember);
        } catch (Exception e) {
            log.error("保存或修改信息出错", e);
        }
        return "savesuccess";
    }
    
    
    public String delete() {
        String id = RequestUtil.getParam(request, "id", "");
        boolean isFlag = false;
        isFlag = service.delete(id);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        if (isFlag)
            message = "删除成功";
        else
            message = "删除失败";
        StringBuffer sb = new StringBuffer("[");
        sb.append("{\"message\":\"" + message + "\"" + "}");
        sb.append("]");
        
        try {
            out = response.getWriter();
            out.println(sb.toString());
        }
        catch (IOException e) {
            log.error("删除信息出错", e);
        }
        return null;
    }
    
    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public TsMember getTsMember() {
        return tsMember;
    }

    public void setTsMember(TsMember tsMember) {
        this.tsMember = tsMember;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }
    
    
    
}
